<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/10/13
 * Time: 9:14
 */
namespace App\Http\Controllers\Api;

use App\Service\Api\CartService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Constant\ErrorConstant;

class CartController extends BaseController
{
    protected $service;

    public function __construct(Request $request, CartService $service)
    {
        parent::__construct($request);
        $this->service  =   $service;
    }

    /**
     * 用户购物车列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function listCart()
    {
        [$flag, $result]    =   $this->service->cartList();
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_CART_LIST_ERROR, $result);
        }
    }

    /**
     * 用户添加购物车
     * @return \Illuminate\Http\JsonResponse
     */
    public function collectCart()
    {
        $validator  =   Validator::make($this->request->post(), [
            'good_id'   =>  'required|integer|exists:mini_product,id',
            'spec_id'   =>  'integer|exists:mini_product_specs,id',
            'good_number'=> 'required|integer'
        ], [
            'good_id.required'  =>  '商品ID不能为空',
            'good_id.integer'   =>  '商品ID必须为整数',
            'good_id.exists'    =>  '商品不存在',

            'spec_id.required'  =>  '规格ID不能为空',
            'spec_id.integer'   =>  '规格ID必须为整数',
            'spec_id.exists'    =>  '规格不存在',

            'good_number.required'  =>  '商品数量不能为空',
            'good_number.integer'   =>  '商品数量必须为整数',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_CART_COLLECT_ERROR, $validator->errors()->first());
        }

        $good_id    =   (int) $this->request->post('good_id');
        $spec_id    =   (int) $this->request->post('spec_id', 0);
        $good_number=   (int) $this->request->post('good_number');

        [$flag, $result]    =   $this->service->cartCollect($good_id, $spec_id, $good_number);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_CART_COLLECT_ERROR, $result);
        }
    }

    /**
     * 用户删除购物车
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteCart()
    {
        $validator  =   Validator::make($this->request->post(), [
            'collect_at'   =>  'required|array',
        ], [
            'collect_at.required'  =>  '收藏时间不能为空',
            'collect_at.array'     =>  '收藏时间必须为数组',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_CART_DELETE_ERROR, $validator->errors()->first());
        }

        $collect_at    =   $this->request->post('collect_at', []);

        [$flag, $result]    =   $this->service->cartDelete($collect_at);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_CART_DELETE_ERROR, $result);
        }
    }

    /**
     * 修改购物车内容
     * @return \Illuminate\Http\JsonResponse
     */
    public function modifyCart()
    {
        $validator  =   Validator::make($this->request->post(), [
            'collect_at'   =>  'required|integer',
            'good_number'=> 'required|integer'
        ], [
            'collect_at.required'  =>  '收藏时间不能为空',
            'collect_at.integer'   =>  '收藏时间必须为整数',

            'good_number.required' =>  '商品数量不能为空',
            'good_number.integer'  =>  '商品数量必须为整数',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_CART_DELETE_ERROR, $validator->errors()->first());
        }

        $collect_at    =   $this->request->post('collect_at');
        $good_number   =   $this->request->post('good_number');

        [$flag, $result]    =   $this->service->cartModify((int) $collect_at, (int) $good_number);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_CART_DELETE_ERROR, $result);
        }
    }

    /**
     * 积分兑换
     * @return \Illuminate\Http\JsonResponse
     */
    public function integralCart()
    {
        $validator  =   Validator::make($this->request->post(), [
            'good_id'   =>  'required|integer|exists:mini_product_integral,good_id',
            'spec_id'   =>  'required|integer|exists:mini_product_integral,spec_id',
            'address_id'=>  'integer|exists:mini_user_address,id',
            'number'    =>  'required|integer'
        ], [
            'good_id.required'  =>  '商品ID不能为空',
            'good_id.integer'   =>  '商品ID必须为整型',
            'good_id.exists'    =>  '商品不存在',
            'spec_id.required'  =>  '规格ID不能为空',
            'spec_id.integer'   =>  '规格ID必须为整型',
            'spec_id.exists'    =>  '规格不存在',
//            'address_id.required'=> '用户地址ID不能为空',
            'address_id.integer'=>  '用户地址ID必须为整型',
            'address_id.exists' =>  '用户地址不存在',
            'number.required'   =>  '商品数量不能为空',
            'number.integer'    =>  '商品数量必须为整型'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_CART_INTEGRAL_ERROR, $validator->errors()->first());
        }

        $good_id    =   (int) $this->request->post('good_id');
        $spec_id    =   (int) $this->request->post('spec_id');
        $number     =   (int) $this->request->post('number');
        $address_id =   $this->request->post('address_id', null);

        [$flag, $result]    =   $this->service->cartIntegral($good_id, $spec_id, $number, $address_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_CART_INTEGRAL_ERROR, $result);
        }
    }
}
